package com.jhws.common.base.service;

import com.jhws.business.car.bo.ParkingLot;
import com.jhws.business.car.service.CarEnteringRecordServic;
import com.jhws.business.car.service.ParkingLotService;
import com.jhws.business.customer.bo.CustConstants;
import com.jhws.business.device.service.DeviceService;
import com.jhws.business.feedback.bo.FeedbackConstant;
import com.jhws.business.feedback.service.FeedbackService;
import com.jhws.business.repair.bo.RepairConstant;
import com.jhws.common.base.bo.Global;
import com.jhws.common.pay.bo.PayConstants;
import com.jhws.core.db.BaseDao;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仪表盘面板数据相关接口
 */
@Service
public class DashBoardService {

    @Resource
    BaseDao BaseDao;
    @Resource
    ParkingLotService parkingLotService;
    @Resource
    CarEnteringRecordServic carEnteringRecordService;
    @Resource
    DeviceService deviceService;
    @Resource
    FeedbackService feedbackService;

    /**
     * 获取楼栋相关统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public List<Integer> getUnitsInfo(String commSql){
        String where = "";
        if(StringUtils.isNotEmpty(commSql))
            where = " where comm_code in ("+commSql+")";
        return BaseDao.findList("select b.unitNumber,b.roomNumber from TblBuildingDimBo as b "+where);
    }

    /**
     * 获取业主相关信息的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public List<Map<String,Object>> getOwnersInfo(String commSql){
        String where = "";
        if(StringUtils.isNotEmpty(commSql))
            where = " where comm_code in ("+commSql+")";
        return BaseDao.findListBySql("select apply_type as applyType, count(owner_code) as count from tbl_owner_dim "+where+" group by apply_type");
    }

    /**
     * 获取停车场相关信息的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public Map getParkingLots(String commSql){
        String where = "";
        if(StringUtils.isNotEmpty(commSql))
            where = " where comm_code in ("+commSql+")";
        Map retMap = new HashMap();
        List<ParkingLot> parkingLots = parkingLotService.queryParkingLostByWhere(where);

        for (ParkingLot parkingLot : parkingLots) {
            if (StringUtils.isNotEmpty(commSql)) {
                long carExist = carEnteringRecordService.queryCarEnteringRecordSize(parkingLot.getParkId());
                retMap.put(parkingLot.getParkId(), parkingLot.getParkSpaceNum() + "-" + carExist);
            }else{
                retMap.put(parkingLot.getParkId(), parkingLot.getParkSpaceNum() +"-0");
            }
        }

        return retMap;
    }

    /**
     * 获取梯口机在线相关信息的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public List getDevsInfo(String commSql){
        if(StringUtils.isEmpty(commSql))
            return null;
        commSql = commSql.replace("'","");
        String [] comms = commSql.split(",");
        List<Map> list = new ArrayList<Map>();
        for(String comm : comms){
            Map ret = deviceService.getDeviceListByComm(comm);
            list.add(ret);
        }
        return list;
    }

    /**
     * 获取IC卡相关信息的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public List<Map<String,Object>> getICCardInfo(String commSql){
        return BaseDao.findListBySql("select state,count(cardNo) as count from card_info where commCode in ("+commSql+") group by state");
    }

    /**
     * 获取新的报修信息的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public Long getNewRepair(String commSql){
        return BaseDao.getCountByHql("select count(id) from Repair where comm_code in ("+commSql+") and state = '"+ RepairConstant.REPAIR_STATE_UNTREATED+"'");
    }

    /**
     * 获取当前小区所有的为缴费用户的统计数据
     * @param commSql 小区的sql字段，为空表示admin账号
     * @return
     */
    public Long getPropertyArrearages(String commSql){
        return BaseDao.getCountByHql("select count(id) from PropertyFeeInfo where comm_code in ("+commSql+") and is_pay = '0'");
    }

    /**
     * 获取客户的反馈数据条目，只有admin角色使用
     * @return
     */
    public Integer findFeedbackCount() throws Exception{
        return feedbackService.findFeedbackCount("where state='" + FeedbackConstant.FEEDBACK_STATE_NOT_HANDLED + "'");
    }

    /**
     * 获取客户提交的支付宝代签账号
     * @return
     */
    public Long findAliPayApplying(){
        return BaseDao.getCountByHql("select count(id) from PayCustomerSecret where state = '"+ CustConstants.PAY_SECRET_STATE_APPLY+"'");
    }
}
